package video.dali.common.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import video.dali.common.interceptor.LoginCheckInterceptor;
import video.dali.common.json.JacksonObjectMapper;

import java.util.List;
@Configuration
@Slf4j
public class WebConfig extends WebMvcConfigurationSupport {

    @Autowired
    private LoginCheckInterceptor loginCheckInterceptor;

    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginCheckInterceptor).excludePathPatterns("/login").addPathPatterns("/**");
    }


    /**
     * 资源拦截器
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("拦截静态资源访问");
        registry.addResourceHandler("/static/videos/dager/**")
                .addResourceLocations("classpath:static/videos/dager/");
        registry.addResourceHandler("/static/images/**")
                .addResourceLocations("classpath:static/images/");
    }


    /**
     * 扩展消息转换器
     * @param converters
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //为消息转换器设置一个对象转换器，对象转换器可以将java对象转换成json数据
        //对象转换器需要自己配置
        converter.setObjectMapper(new JacksonObjectMapper());
        //将消息转换器添加到springMVC框架
        converters.add(0,converter);
    }
}
